﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace AUV5.Web.Service
{
    public partial class OrderPagedList :PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int pagesize = 0;
            int pageindex = 0;
            int status = 0;
            try
            {
                pagesize = int.Parse(Request["pagesize"]??"0");
                pageindex = int.Parse(Request["pageindex"]??"0");
                status = int.Parse(Request["status"] ?? "0");
            }
            catch (Exception)
            {
                Response.Write("113");//无效的整型数值
                return;
            }
            string buyerid = Request["buyerid"] ?? "";
            string sellerid = Request["sellerid"] ?? "";
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey,pageindex, pagesize, status, buyerid, sellerid))
            {
                Response.Write("112");//密钥不正确 
                return;
            }

            if (Request["pagesize"] != null && Request["pageindex"] != null)
            {
                string sqlwhere="State = " + Convert.ToInt32(AUV5.Common.Enum.auvState.Enabled) + " and IsDel = " + Convert.ToInt32(AUV5.Common.Enum.auvDel.NotDel);
                
                if (!string.IsNullOrEmpty(buyerid))
                {
                    Model.UserInfo buyer = BLL.UserInfoBLL.Instance.GetDetail(buyerid);
                    if (buyer == null)
                    {
                        Response.Write("301");//无效的用户
                        return;
                    }
                    else
                    {
                        sqlwhere += " and Buyer='" + buyerid + "'";
                    }
                }
                if (!string.IsNullOrEmpty(sellerid))
                {
                    Model.UserInfo seller = BLL.UserInfoBLL.Instance.GetDetail(sellerid);
                    if (seller == null)
                    {
                        Response.Write("301");//无效的用户
                        return;
                    }
                    else
                    {
                        sqlwhere += " and Seller='" + sellerid + "'";
                    }
                }
                //服务与订单不直接相关，而是与订单项直接相关
                //string serviceid = Request["serviceid"] ?? "";
                //if (!string.IsNullOrEmpty(serviceid))
                //{
                //    Model.ServiceInfo service = BLL.ServiceInfoBLL.Instance.GetDetail(serviceid);
                //    if (service == null)
                //    {
                //        Response.Write("300");//无效的服务 
                //        return;
                //    }
                //}
               
                if (status != 0)
                {
                    sqlwhere += " and StatusID=" + status;
                }
                Model.NetPager pager = new Model.NetPager();
                pager.PageIndex = pageindex;
                pager.TableName = "OrderInfo";
                pager.PrimaryKey = "OrderID";
                pager.Where = sqlwhere;
                pager.Order = "CreateDate desc,OrderID desc";
                pager.SortType = (int)Common.Enum.SortType.Multi;
                pager.PageSize = pagesize;

                
               IList<Model.OrderInfo> list= BLL.OrderInfoBLL.Instance.GetOrderListPage(pager);

                //获取订单项
               foreach (var item in list)
               {
                   item.ItemList = new List<Model.OrderItem>();
                   item.ItemList.Add(BLL.OrderItemBLL.Instance.GetOrderItemByOrderID(item.OrderID));
               }
               Hashtable ht = new Hashtable();
               ht.Add("RecordCount", pager.TotalCount);
               ht.Add("OrderList", list);
               Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(ht));
            }
            else
            {
                Response.Write("111");//参数不正确
            }
        }

        protected bool IsValidKey(string secretkey,int pageindex, int pagesize, int status, string buyerid, string sellerid)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = base.EncryptMD5(public_key + ((pageindex + pagesize) * status).ToString() + buyerid + sellerid + current_day).Substring(6, 22);
                    return finalkey == secretkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}